package com.share.bigdata.hdfs.service.impl;

import com.share.bigdata.hdfs.service.HdfsService;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

@Service
public class HdfsServiceImpl implements HdfsService {

    @Autowired
    private FileSystem fileSystem;

    @Override
    public List<String> listFiles(String path) throws IOException {
        List<String> reList = new LinkedList<>();
        // 列出HDFS根目录下的文件和目录
        RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/"), true);

        while (files.hasNext()) {
            LocatedFileStatus next = files.next();
            String name = next.getPath().getName();
            reList.add(name);
        }

        return reList;
    }
}
